package com.water.servlet;

import com.water.dao.WaterDao;
import com.water.dao.impl.WaterDaoImpl;
import com.water.domain.UserBean;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collections;

public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            String username = req.getParameter("name");
            String password = req.getParameter("password");
            String pwd = req.getParameter("pwd");
            System.out.println("username:" + username + "====password:" + password);
            if (password == pwd) {
                UserBean reg = new UserBean();
                reg.setUsername(username);
                reg.setPassword(password);
                WaterDao dao = new WaterDaoImpl();
                dao.register(reg);
                resp.sendRedirect("jsp/login.jsp");
            } else {
                resp.sendRedirect("jsp/register.jsp");
                System.out.println("重新输入");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doGet(req, resp);
    }

    @Override
    public void destroy() {
        super.destroy();
    }
}
